package com.semidata.trp.monitor.config;

import java.io.File;

import com.semidata.rtd.core.config.DefaultValue;
import com.semidata.rtd.core.config.DefaultValueClass;
import com.semidata.rtd.core.config.DefaultValueHandler;
import com.semidata.rtd.core.config.Handler;
import com.semidata.rtd.core.config.Property;
import com.semidata.rtd.core.config.UrlHandler;
import com.semidata.rtd.core.datasource.redis.RedisDataSource;

public class Config extends com.semidata.rtd.core.config.Config {
    private static String DEFAULT_CONFIG_FILE = "conf/monitor.conf";
    private static Config config = null;

    public static void init(String configFile) {
        if (configFile == null)
            configFile = Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath();
        config = new Config(configFile);
    }

    public static class DefaultLogDir implements DefaultValueHandler {

        @Override
        public String value() {
            File file = new File(Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath());
            file = new File(file.getParentFile().getParentFile(), "logs/monitor");
            return file.getAbsolutePath();
        }

    }

    public static class DefaultDataDir implements DefaultValueHandler {

        @Override
        public String value() {
            File file = new File(Thread.currentThread().getContextClassLoader().getResource(DEFAULT_CONFIG_FILE).getPath());
            file = new File(file.getParentFile().getParentFile(), "data/monitor");
            return file.getAbsolutePath();
        }

    }
    
    public static Config getConfig() {
        return config;
    }

    private Config(String configFile) {
        initConfig(configFile);
    }

    @Property("http.port")
    @DefaultValue("6100")
    private int httpPort;

    @Property("http.threads")
    @DefaultValue("16")
    private int httpThreads;

    @Property("log.root")
    @DefaultValueClass(DefaultLogDir.class)
    private String logRoot;

    @Property("log.debug.enabled")
    @DefaultValue("false")
    private boolean logDebugEnabled;

    @Property("url.prefix.impression")
    @DefaultValue("/a.gif/")
    @Handler(UrlHandler.class)
    private String impressionPrefix;

    @Property("url.prefix.click")
    @DefaultValue("/r.gif/")
    @Handler(UrlHandler.class)
    private String clickPrefix;

    @Property("url.business")
    @Handler(UrlHandler.class)
    private String businessUrl;

    @Property("monitor.id")
    private String monitorId;

    @Property("report.data.dir")
    @DefaultValueClass(DefaultDataDir.class)
    private String reportDataDir;
    
    @Property("redis.hosts")
    @DefaultValue("127.0.0.1:3306")
    private String redisHosts;
    
    @Property("redis.connect.timeout")
	@DefaultValue("2000")
	private int redisConnectTimeout;
	
	@Property("redis.task.timeout")
	@DefaultValue("50")
	private int redisTaskTimeout;
	
	@Property("redis.threads")
	@DefaultValue("16")
	private int redisThreads;
	
	@Property("redis.connect.num")
	@DefaultValue("3")
	private int redisConnectNum;
	
    public int getRedisConnectTimeout() {
		return redisConnectTimeout;
	}

	public int getRedisTaskTimeout() {
		return redisTaskTimeout;
	}

	public int getRedisThreads() {
		return redisThreads;
	}

	public int getRedisConnectNum() {
		return redisConnectNum;
	}

	private RedisDataSource datasource;
    
    public void setRedisDataSource(RedisDataSource datasource) {
    	this.datasource = datasource;
    }
    
    public RedisDataSource getRedisDataSource() {
    	return datasource;
    }
    
    public String[] getRedisHosts() {
    	return redisHosts.split(";");
    }

    public String getReportDataDir() {
        return reportDataDir;
    }

    public int getHttpPort() {
        return httpPort;
    }

    public int getHttpThreads() {
        return httpThreads;
    }

    public String getLogRoot() {
        return logRoot;
    }

    public boolean isLogDebugEnabled() {
        return logDebugEnabled;
    }

    public String getImpressionPrefix() {
        return impressionPrefix;
    }

    public String getClickPrefix() {
        return clickPrefix;
    }

    public String getBusinessUrl() {
        return businessUrl;
    }

    public String getMonitorId() {
        return monitorId;
    }
    
    @Override
    public String toString() {
        return "Config [httpPort=" + httpPort + ", httpThreads=" + httpThreads + ", logRoot=" + logRoot + ", logDebugEnabled=" + logDebugEnabled + ", impressionPrefix=" + impressionPrefix
                + ", clickPrefix=" + clickPrefix + ", businessUrl=" + businessUrl + ", monitorId=" + monitorId + ", reportDataDir=" + reportDataDir + "]";
    }

    public static void main(String[] args) {
        init(null);
        System.out.println(getConfig().toString());
    }

}
